package com.summer.safe.config.configure;

import com.summer.safe.config.aop.log.LogInterceptor;
import com.summer.safe.config.listener.ConfigListener;
import com.summer.safe.utils.ToolUtil;
import com.summer.safe.config.properties.ConfigureProperties;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 *
 * MVC配置类
 *
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Resource
    private ConfigureProperties configureProperties;


    /**
     * 默认访问页面
     * @param reg
     */
    @Override
    public void addViewControllers(ViewControllerRegistry reg) {
        //默认访问页面
        reg.addViewController("/").setViewName("forward:/login");
    }
    /**
     * 静态资源的配置
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        //如果是Windows系统
        if(ToolUtil.getSystemOs()){
            //addResourceHandler:访问路径  映射的路径
            //addResourceLocations:文件放置的目录 文件存储的绝对路径
            registry.addResourceHandler("/uploadfiles/**").addResourceLocations("file:" + configureProperties.getFileUploadPath() + "/uploadfiles/");
        }else{
            //linux 和mac
            registry.addResourceHandler("/uploadfiles/**").addResourceLocations("file:" + configureProperties.getFileUploadPathLinux()+ "/uploadfiles/");
        }

    }

    /**
     * 自定义拦截规则
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LogInterceptor()).addPathPatterns("/**")
                .excludePathPatterns("/")
                .excludePathPatterns("/login")
                .excludePathPatterns("/static/**")
                .excludePathPatterns("/sys/menu/treeData");


    }

    /**
     * ConfigListener注册
     */
    @Bean
    public ServletListenerRegistrationBean<ConfigListener> configListenerRegistration() {
        return new ServletListenerRegistrationBean<>(new ConfigListener());
    }




}
